package n.d.l.svn.web;

import java.io.IOException;
import java.text.ParseException;

import n.d.l.svn.bean.Patch;
import n.d.l.svn.bean.Project;
import n.d.l.svn.bean.ResponseBean;
import n.d.l.svn.service.PatchService;
import n.d.l.svn.service.ProjectService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("patch")
public class PatchController {

	@Autowired
	private ProjectService projectService;
	@Autowired
	private PatchService patchService;
	
	@RequestMapping(value="tpp", method=RequestMethod.GET)
	public String toPatchPage(int projectId, Model model) throws IOException, ParseException{
		Project project = projectService.getById(projectId);
		model.addAttribute("pro", project);
		model.addAttribute("svnLogs", patchService.findSvnLogs(project));
		return "patch";
	}
	
	/**
	 * 生产打包目录.
	 * @param versionStart
	 * @param versionEnd
	 * @param projectId
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value="diff", method=RequestMethod.POST)
	@ResponseBody
	public Patch diff(String versionStart, String versionEnd, int projectId) throws IOException, ParseException {
		Project project = projectService.getById(projectId);
		return patchService.diff(versionStart, versionEnd, project);
	}
	
	/**
	 * 打包.
	 * @param versionStart
	 * @param versionEnd
	 * @param projectId
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="dop", method=RequestMethod.POST)
	@ResponseBody
	public ResponseBean doPatch(String patchFile, String ignoreFile, String removeFile, int projectId, String versionStart, String versionEnd) throws IOException{
		Project project = projectService.getById(projectId);
		return patchService.doPatch(patchFile, ignoreFile, removeFile, project, versionStart, versionEnd);
	}
}
